package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.website.annotion.RequireLogin;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Reference
    private IUserInfoService userInfoService;

    @RequireLogin
    @RequestMapping("/hello")
    @ResponseBody
    public Object hello(Long id) {
        return userInfoService.get(id);
    }

    //手机号码唯一性检查
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {

        return !userInfoService.checkPhone(phone);
    }


    //短信验证码的发送
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    //用户注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判断用户是否注册成功
            userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
        return AjaxResult.SUCCESS;
    }

    //  用户登录
    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request, HttpServletResponse response, String username, String password){
        String token = userInfoService.userLogin(username,password);

        //5.创建cookie，以token的名为name，值为value
        CookieUtil.addCookie("token",token,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue(),response);


        return AjaxResult.SUCCESS;
    }
    //用户登录
/*    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request,HttpServletResponse response,String username, String password)throws LogicException {
        //判断用户是否注册成功
         String token=userInfoService.userLogin(username, password);
        //5:创建cookie对象,以token的值作为value  这个是表现层的业务
        //cookie c = new cookie("token",token变量对应的随机值),设置cookie有效时间30分钟,在同response对象传到浏览器中
        //设置存活时间
        int time = RedisKeys.USER_LOGIN_TOKEN.getTime().intValue();
        CookieUtil.addCookie("token",token,time,response);
        return AjaxResult.SUCCESS;
    }*/
}
